import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_boost/boost_navigator.dart';
import 'package:flutter_boost/flutter_boost_app.dart';
import 'package:food/config/build_config.dart';
import 'package:food/home.dart';
import 'package:food/model/home_tab_view_model.dart';
import 'package:food/ui/location_info.dart';
import 'package:food/ui/search_info.dart';
import 'package:food/ui/store_info.dart';
import 'package:food/ui/store_tab_view_order_details.dart';
import 'package:food/ui/system_settings.dart';
import 'package:food/ui/transport_map.dart';
import 'package:food/ui/user_info.dart';

import 'flutter_page.dart';

void main() => runApp(App());

class App extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return AppState();
  }
}

class AppState extends State<App> {
  static Map<String, FlutterBoostRouteFactory> routerMap = {
    'home': (settings, uniqueId) {
      return PageRouteBuilder<dynamic>(
          settings: settings,
          pageBuilder: (_, __, ___) => Home());
    },
    'system_settings': (settings, uniqueId) {
      return PageRouteBuilder<dynamic>(
          settings: settings,
          pageBuilder: (_, __, ___) => SystemSettings());
    },
    'product_info': (settings, uniqueId) {
      return PageRouteBuilder<dynamic>(
          settings: settings,
          pageBuilder: (_, __, ___) => StoreTabViewOrderDetails());
    },

    'transport_map': (settings, uniqueId) {
      return PageRouteBuilder<dynamic>(
          settings: settings,
          pageBuilder: (_, __, ___) => TransportMap());
    },
    'search_info': (settings, uniqueId) {
      return PageRouteBuilder<dynamic>(
          settings: settings,
          pageBuilder: (_, __, ___) => SearchInfo());
    },
    'user_info': (settings, uniqueId) {
      Map<String, dynamic> datas = settings.arguments;
      return PageRouteBuilder<dynamic>(
          settings: settings,
          pageBuilder: (_, __, ___) => UserInfo(
           path: datas['path'].toString(),
          ));
    },
    'store_info': (settings, uniqueId) {
      Map<String, dynamic> datas = settings.arguments;
      var  mapModel = jsonDecode(datas['HomeTabViewModel']);
      var model = HomeTabViewModel.fromJson(mapModel);
      return PageRouteBuilder<dynamic>(
          settings: settings,
          pageBuilder: (_, __, ___) => StoreInfo(
            model,
          ));
    },
    'location_info': (settings, uniqueId) {
      Map<String, dynamic> datas = settings.arguments;
      return PageRouteBuilder<dynamic>(
          settings: settings,
          pageBuilder: (_, __, ___) => LocationInfo(
            address: datas['address'],
          ));
    },
  };
  Route<dynamic> routeFactory(RouteSettings settings, String uniqueId) {
    FlutterBoostRouteFactory func =routerMap[settings.name];
    if (func == null) {
      return null;
    }
    return func(settings, uniqueId);
  }


  void _onRoutePushed(
      String pageName, String uniqueId, Map params, Route route, Future _) {}

  @override
  void initState() {
    super.initState();

  }

  @override
  Widget build(BuildContext context) {
    // return MaterialApp(
    //   title: 'Flutter Hybrid',
    //   builder: FlutterBoost.init(postPush: _onRoutePushed),
    //   home: BuildConfig.isDev ? Home() : Container(),
    // );
    return FlutterBoostApp(
        routeFactory
    );
  }
}
